<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="河流名称" prop="riverName">
        <el-input
          v-model="queryParams.riverName"
          placeholder="请输入河流名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="河长" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入河长姓名"
          clearable
          @keyup.enter.native="haxndleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['riv:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['riv:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['riv:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['riv:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="河流名称" align="center" prop="riverName" />
      <el-table-column label="河流图片" align="center" prop="riverImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.riverImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="河道描述" align="center" prop="info" />
      <el-table-column label="河道状况" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.river_info_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="河长名称" align="center" prop="userName" />
      <el-table-column label="记录用户名称" align="center" prop="leaderName" />
      <el-table-column label="记录时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="更新用户名称" align="center" prop="updateUser" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['riv:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['riv:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改河流信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="河流名称" prop="riverName">
          <el-input v-model="form.riverName" placeholder="请输入河流名称" />
        </el-form-item>

        <el-form-item label="河长" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择河长" @change="leaderChange">
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.userName"
              :value="parseInt(dict.userId)"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门"/>
        </el-form-item>

        <el-form-item label="河流图片" prop="riverImg">
          <image-upload v-model="form.riverImg"/>
        </el-form-item>
        <el-form-item label="河道描述" prop="info">
          <el-input v-model="form.info" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="河道状况" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.river_info_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>


        <baidu-map class="river-map" :center="{lng: 105.9409, lat: 29.3722}" :zoom="15">
          <bm-polygon :path="polygonPath" stroke-color="blue" :stroke-opacity="0.5" :stroke-weight="2" :editing="true" @lineupdate="updatePolygonPath"/>
        </baidu-map>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/riv/info";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect } from "@/api/system/user";
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import Treeselect from '@riophae/vue-treeselect'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";



export default {
  name: "Info",
  dicts: ['river_info_status'],
  components: { Treeselect },
  data() {
    return {
      deptOptions: undefined,
      deptList:{},
      userList:{},
      userTotal:0,
      polygonPath: [
        {lng: 105.937974, lat: 29.375009},
        {lng: 105.936034, lat: 29.370602},
        {lng: 105.942646, lat: 29.371358}
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 河流信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        riverName: null,
        userName: null,
        leaderName: null,
      },
      leaderName: "",
      deptName: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        riverName: [
          {required: true, message: "河流名称不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "河道状况不能为空", trigger: "change"}
        ],
        userId: [
          {required: true, message: "河长ID不能为空", trigger: "blur"}
        ],
        deptId: [
          {required: true, message: "部门ID不能为空", trigger: "blur"}
        ],



      }
    };
  },
  created() {
    this.getList();
    this.getUserList();
    this.getDeptList();
    this.getDeptTree();
  },
  methods: {
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },

    /** 查询部门列表 */
    getDeptList() {
      this.loading = true;
      listDept(this.queryParams).then(response => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.loading = false;
      });
    },


    /** 查询用户列表 */
    getUserList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.userTotal = response.total;
          this.loading = false;
        }
      );
    },

    // 输入河长名字
    leaderChange(r){
      this.userList.forEach(item => {
        if(item.userId == r){
          this.userName = item.userName
        }
      })
    },

    // 输入部门名字
    deptChange(){

      this.deptOptions.forEach(item => {
        if(item.deptId == this.form.deptId){
          console.log("123")
          this.form.deptName = item.deptName
        }
      })
    },

    updatePolygonPath (e) {
      this.polygonPath = e.target.getPath()
    },
    /** 查询河流信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        riverId: null,
        riverName: null,
        riverImg: null,
        info: null,
        status: null,
        userId: null,
        userName: null,
        createTime: null,
        updateId: null,
        updateUser: null,
        updateTime: null,
        leaderId: null,
        //leaderName: null,
       // deptId: null,
        deptName: null,
        coordinate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.riverId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加河流信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
     // this.reset();

      this.form= row;
      // 输入河道JSON
      this.polygonPath = JSON.parse(row.coordinate)



      this.open = true;
      this.title = "修改河流信息";
      // const riverId = row.riverId || this.ids
      // getInfo(riverId).then(response => {
      //   this.form = response.data;
      //
      // });
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.coordinate = JSON.stringify(this.polygonPath);

          // 创建者
          if(this.form.leaderId == null || this.form.leaderId === ""){
            this.form.leaderId = this.$store.state.user.id;
            this.form.leaderName = this.$store.state.user.name;
          }else{
            this.form.updateId = this.$store.state.user.id;
            this.form.updateUser = this.$store.state.user.name;
          }

          this.deptChange();
          // 更新者

          // 河长和部门
          this.form.userName = this.userName;



          if (this.form.riverId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {

            addInfo(this.form).then(response => {

              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const riverIds = row.riverId || this.ids;
      this.$modal.confirm('是否确认删除河流信息编号为"' + riverIds + '"的数据项？').then(function() {
        return delInfo(riverIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('riv/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.river-map{
  width: 100%;
  height: 400px;
}
</style>
